package com.fr.data.impl;

import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.io.core.ExcelUtils;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFCell;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFRow;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.fr.third.org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelTableData extends AbstractTableData
  implements XMLable
{
  private DirectoryConnection directoryConnection;
  private String filePath = null;
  private List columnNameList = new ArrayList();
  private boolean needColumnName;
  private List rowDataList = new ArrayList();
  private boolean isInitialed = false;

  public DirectoryConnection getDirectoryConnection()
  {
    return this.directoryConnection;
  }

  public void setDirectoryConnection(DirectoryConnection paramDirectoryConnection)
  {
    this.directoryConnection = paramDirectoryConnection;
  }

  public String getFilePath()
  {
    return this.filePath;
  }

  public void setFilePath(String paramString)
  {
    this.filePath = paramString;
  }

  public boolean needColumnName()
  {
    return this.needColumnName;
  }

  public void setNeedColumnName(boolean paramBoolean)
  {
    this.needColumnName = paramBoolean;
  }

  public int getColumnCount()
    throws TableDataException
  {
    loadColumnName();
    return this.columnNameList.size();
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    loadColumnName();
    Object localObject = (paramInt < this.columnNameList.size()) ? this.columnNameList.get(paramInt).toString() : null;
    return localObject;
  }

  public int getRowCount()
    throws TableDataException
  {
    initial();
    return this.rowDataList.size();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    initial();
    List localList = (List)this.rowDataList.get(paramInt1);
    return ((paramInt2 < localList.size()) ? localList.get(paramInt2) : null);
  }

  private void loadColumnName()
  {
    HSSFWorkbook localHSSFWorkbook;
    HSSFSheet localHSSFSheet;
    Iterator localIterator1;
    HSSFRow localHSSFRow;
    Iterator localIterator2;
    HSSFCell localHSSFCell;
    File localFile = new File(this.directoryConnection.getLocation());
    if (!(localFile.isDirectory()))
      return;
    ArrayList localArrayList = new ArrayList();
    this.columnNameList.clear();
    if (this.needColumnName)
    {
      try
      {
        POIFSFileSystem localPOIFSFileSystem1 = new POIFSFileSystem(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath })));
        localHSSFWorkbook = new HSSFWorkbook(localPOIFSFileSystem1);
        localHSSFSheet = localHSSFWorkbook.getSheetAt(0);
        localIterator1 = localHSSFSheet.rowIterator();
        if (localIterator1.hasNext())
        {
          localHSSFRow = (HSSFRow)localIterator1.next();
          localIterator2 = localHSSFRow.cellIterator();
          while (localIterator2.hasNext())
          {
            localHSSFCell = (HSSFCell)localIterator2.next();
            this.columnNameList.add(ExcelUtils.getHSSFCellValue(localHSSFCell));
          }
        }
      }
      catch (FileNotFoundException localFileNotFoundException1)
      {
        localFileNotFoundException1.printStackTrace();
      }
      catch (IOException localIOException1)
      {
        localIOException1.printStackTrace();
      }
    }
    else
    {
      try
      {
        POIFSFileSystem localPOIFSFileSystem2 = new POIFSFileSystem(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath })));
        localHSSFWorkbook = new HSSFWorkbook(localPOIFSFileSystem2);
        localHSSFSheet = localHSSFWorkbook.getSheetAt(0);
        localIterator1 = localHSSFSheet.rowIterator();
        if (localIterator1.hasNext())
        {
          localHSSFRow = (HSSFRow)localIterator1.next();
          localIterator2 = localHSSFRow.cellIterator();
          while (localIterator2.hasNext())
          {
            localHSSFCell = (HSSFCell)localIterator2.next();
            this.columnNameList.add(ExcelUtils.getHSSFCellValue(localHSSFCell));
          }
        }
      }
      catch (FileNotFoundException localFileNotFoundException2)
      {
        localFileNotFoundException2.printStackTrace();
      }
      catch (IOException localIOException2)
      {
        localIOException2.printStackTrace();
      }
      for (int i = 0; i < this.columnNameList.size(); ++i)
        localArrayList.add("#" + (i + 1));
      this.columnNameList = localArrayList;
    }
  }

  private void initial()
  {
    HSSFWorkbook localHSSFWorkbook;
    int j;
    if (this.isInitialed)
      return;
    File localFile = new File(this.directoryConnection.getLocation());
    if (!(localFile.isDirectory()))
      return;
    this.rowDataList.clear();
    try
    {
      POIFSFileSystem localPOIFSFileSystem = new POIFSFileSystem(new FileInputStream(BaseCoreUtils.pathJoin(new String[] { localFile.getPath(), this.filePath })));
      localHSSFWorkbook = new HSSFWorkbook(localPOIFSFileSystem);
      int i = localHSSFWorkbook.getNumberOfSheets();
      for (j = 0; j < i; ++j)
      {
        HSSFSheet localHSSFSheet = localHSSFWorkbook.getSheetAt(j);
        Iterator localIterator1 = localHSSFSheet.rowIterator();
        while (localIterator1.hasNext())
        {
          HSSFRow localHSSFRow = (HSSFRow)localIterator1.next();
          ArrayList localArrayList = new ArrayList();
          Iterator localIterator2 = localHSSFRow.cellIterator();
          while (localIterator2.hasNext())
          {
            HSSFCell localHSSFCell = (HSSFCell)localIterator2.next();
            localArrayList.add(ExcelUtils.getHSSFCellValue(localHSSFCell));
          }
          this.rowDataList.add(localArrayList);
        }
      }
    }
    catch (FileNotFoundException localFileNotFoundException)
    {
      localFileNotFoundException.printStackTrace();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
    if ((this.needColumnName) && (this.rowDataList.size() > 0))
      this.rowDataList.remove(0);
    this.isInitialed = true;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if ("ExcelTableDataAttr".equals(str1))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("filePath")) != null)
          this.filePath = str2;
        if ((str2 = paramXMLableReader.getAttr("needColumnName")) != null)
          if (str2.equals("true"))
            setNeedColumnName(true);
          else
            setNeedColumnName(false);
      }
      else if ("Connection".equals(str1))
      {
        Connection localConnection = DataXMLUtils.readXMLConnection(paramXMLableReader);
        if (localConnection instanceof DirectoryConnection)
          this.directoryConnection = ((DirectoryConnection)localConnection);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("ExcelTableDataAttr").attr("needColumnName", needColumnName()).attr("filePath", getFilePath()).end();
    if (this.directoryConnection != null)
      DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, this.directoryConnection);
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof ExcelTableData) && (super.equals(paramObject)) && (this.needColumnName == ((ExcelTableData)paramObject).needColumnName) && (ComparatorUtils.equals(this.filePath, ((ExcelTableData)paramObject).filePath)) && (ComparatorUtils.equals(this.directoryConnection, ((ExcelTableData)paramObject).directoryConnection)));
  }
}